package baiqi.sishen.community.service;

import baiqi.sishen.community.dto.NotificationDTO;
import baiqi.sishen.community.enums.NotificationStatusEnum;
import baiqi.sishen.community.enums.NotificationTypeEnum;
import baiqi.sishen.community.execption.CustomizeErrorCode;
import baiqi.sishen.community.execption.CustomizeException;
import baiqi.sishen.community.mapper.NotificationMapper;
import baiqi.sishen.community.model.*;
import baiqi.sishen.community.utils.NavigatePagesUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/5/2
 */
@Service
public class NotificationService {

    @Resource
    private NotificationMapper notificationMapper;

    /**
     * 返回通知 列表
     *
     * @param userId 当前登录用户
     * @param page   当前页
     * @param size   页容量
     * @return PageInfo<NotificationDTO>
     */
    public PageInfo<NotificationDTO> list(Long userId, Integer page, Integer size) {

        NotificationExample notificationExample = new NotificationExample();
        notificationExample.createCriteria()
                .andReceiverEqualTo(userId);
        notificationExample.setOrderByClause("gmt_create desc");

        PageHelper.startPage(page, size);
        List<Notification> projectVOList = notificationMapper.selectByExample(notificationExample);
        PageInfo<Notification> pageInfo = new PageInfo(projectVOList);
        PageInfo<NotificationDTO> notificationDTOPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, notificationDTOPageInfo, "list");
        //导航页
        int[] ints = NavigatePagesUtil.getNavigatePages(page, notificationDTOPageInfo.getPages());
        notificationDTOPageInfo.setNavigatepageNums(ints);

        ArrayList<NotificationDTO> notificationDTOs = new ArrayList<>();
        for (int i = 0; i < pageInfo.getList().size(); i++) {
            NotificationDTO notificationDTO = new NotificationDTO();
            Notification notification = pageInfo.getList().get(i);
            BeanUtils.copyProperties(notification, notificationDTO);
            notificationDTO.setTypeName(NotificationTypeEnum.contentOfType(notification.getType()));
            notificationDTOs.add(notificationDTO);
        }
        notificationDTOPageInfo.setList(notificationDTOs);
        return notificationDTOPageInfo;
    }

    /**
     * 通知设为已读
     *
     * @param id   通知id
     * @param user 当前登录用户
     * @return NotificationDTO
     */
    public Notification read(Long id, User user) {
        //获取通知
        Notification notification = notificationMapper.selectByPrimaryKey(id);
        if (notification == null) {
            throw new CustomizeException(CustomizeErrorCode.NOTIFICATION_NOT_FOUND);
        }

        if (!notification.getReceiver().equals(user.getId())) {
            throw new CustomizeException(CustomizeErrorCode.READ_NOTIFICATION_FAIL);
        }
        //设置已读信号
        notification.setStatus(NotificationStatusEnum.READ.getStatus());
        notificationMapper.updateByPrimaryKey(notification);

        return notification;
    }

    /**
     * 查询当前登录用户的温度通知数量
     *
     * @param userId 当前登录用户id
     * @return Long
     */
    public Long unreadCount(Long userId) {

        NotificationExample notificationExample = new NotificationExample();
        notificationExample.createCriteria()
                .andReceiverEqualTo(userId)
                .andStatusEqualTo(NotificationStatusEnum.UNREAD.getStatus());//未读状态码
        long unreadCount = notificationMapper.countByExample(notificationExample);

        return unreadCount;
    }
}
